<!-- 
	输入查询码页面
 -->
<template>
	<view class="container">
		<view v-if="historyList.length">
			<view class="flex space-between">
				<text class="bold text-md">搜索历史</text>
				<text class="icon iconfont icon-trash text-md" @click="historyDelete"></text>
			</view>
			<view class="flex flex-wrap history-wrap">
				<view v-for="(item, index) in historyList" :key="index" class="history-item" hover-class="active" @click="search(item)">
					<text>{{item}}</text>
				</view>
			</view>
		</view>
		<view v-else class="flex align-center justify-center">
			<text>没有查询过</text>
		</view>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				inputText: '' // 查询内容 
			}
		},
		// 虚拟键盘的搜索按钮点击
		onNavigationBarSearchInputConfirmed(e) {
			this.setStorageList(e.text)
		},
		// 输入监听
		onNavigationBarSearchInputChanged(e) {
			this.inputText = e.text
		},
		// 导航栏按钮点击
		onNavigationBarButtonTap(e) {
			if(e.buttonType == 'search') {
				this.setStorageList(this.inputText)
			}
		},
		onLoad() {
			this.getStorageList()
		},
		computed: {
			...mapState('request-search', {
				historyList: 'historyList' // 搜索记录
			})
		},
		methods: {
			// 缓存查询id, 最大缓存9个
			// 点击按钮查询后清除输入框内容
			setStorageList(id) {
				this.$store.dispatch('request-search/searchHistoryHandler', id)
				// #ifdef APP-PLUS
				let currentWebview = this.$scope.$getAppWebview()
				currentWebview.setTitleNViewSearchInputText('')
				// #endif
				this.$store.dispatch('request-search/search', id)
			},
			// 点击历史查询
			search(id) {
				this.$store.dispatch('request-search/search', id)
			},
			// 加载缓存数据
			getStorageList() {
				this.$storage('get', 'historyList').then(res => {
					try {
						let list = JSON.parse(res)
						this.$store.commit('request-search/LIST_SET', list)
					}catch(e) {}
				})
			},
			// 删除历史记录
			historyDelete() {
				this.$showModal({
					showTitle: false,
					content: '清除历史记录'
				}).then(res => {
					this.$storage('remove', 'historyList').then(res => {
						this.$store.commit('request-search/LIST_CLEAR')
					})
				})
			}
		}
	}
</script>

<style lang="less" scoped>
.history-wrap {
	margin: 20rpx -20rpx 0 0;
}
.history-item {
	background-color: #dee2ef;
	border: 1rpx solid #cecfd0;
	border-radius: 50rpx;
	padding: 6rpx 20rpx;
	margin: 0 20rpx 30rpx 0;
	color: #666;
}
.active {
	background-color: #cdd1de;
}
</style>
